<template>
  <div class="dashboard-container">
    <!-- <canvas id="myCanvas" width="32" height="32">
      您的浏览器版本过低,不支持canvas,请升级浏览器或使用chrome浏览器
    </canvas> -->

    <div class="filter-container">
      <el-select v-model="listQuery.type" style="width:300px;margin-right:10px" placeholder="请选择类型" @change="reloadList">
        <el-option
          label="站内通知"
          :value="1"
        />
        <el-option
          label="新闻公告"
          :value="2"
        />
        <el-option
          label="其他"
          :value="3"
        />
      </el-select>
      <el-button
        size="mini"
        type="primary"
        @click="addNews()"
      >
        新 增
      </el-button>
    </div>
    <div style="padding:0 20px">

      <el-table
        ref="multipleTable"
        v-loading="listLoading"
        :data="list"
        height="calc(100vh - 200px)"
        style="width: 100%;"
        size="mini"
        border
        tooltip-effect="dark"
        :header-cell-style="{ background: '#eaf1fd', color: '#333' }"
      >
        <el-table-column
          label="标题"
          prop="title"
          min-width="600px"
        />
        <!-- <el-table-column
          label="内容"
          min-width="400px"
        >
          <template slot-scope="{ row }">
            <div>
              <span style="color:green">{{ row.content }}</span>
            </div>
          </template>

        </el-table-column> -->
        <el-table-column
          label="创建时间"
          prop="add_time"
          width="160px"
        />
        <el-table-column
          label="更新时间"
          prop="edit_time"
          width="160px"
        />
        <el-table-column
          label="操作"
          align="right"
          width="200"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="{ row }">
            <el-button
              size="mini"
              type="primary"
              @click="edit(row)"
            >
              编辑
            </el-button>
            <el-button
              type="danger"
              @click="delet(row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="getList"
      />
    </div>
    <Detial :detial="detial" :status="status" :visible="visible" @update="closeDraw" @reload="reloadList" />
  </div>
</template>

<script>
import { listNotice, delNotice } from '@/api/dashboard/dashboard'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import Detial from './detial.vue'
import { process } from '@/utils/getCanvasData'
export default {
  name: 'Dashboard',
  components: {
    Pagination,
    Detial
  },
  data() {
    return {
      data_arr: [],
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        type: 1
      },
      visible: false,
      status: 1,
      id: '',
      detial: {}
    }
  },
  computed: {
  },
  created() {
    // this.getCanvas()
    this.getList()
  },
  methods: {
    getCanvas() {
      setTimeout(async() => {
        const canvas = document.getElementById('myCanvas')
        const ctx = canvas.getContext('2d')
        ctx.fillRect(0, 0, 32, 32)
        console.log(canvas)
        const canvasData = await process({
          canvas: canvas,
          width: 32,
          height: 32
        })

        console.log(canvasData)
      }, 100)
    },
    closeDraw() {
      this.visible = false
    },
    addNews() {
      this.status = 1
      this.visible = true
    },
    edit(row) {
      this.detial = { ...row }
      this.status = 2
      this.visible = true
    },
    delet(row) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delNotice({
          notice_id: row.id
        }).then(res => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.getList()
        })
      }).catch(() => {

      })
    },
    reloadList() {
      this.listQuery.page = 1
      this.visible = false
      this.getList()
    },
    getList() {
      this.listLoading = true
      listNotice(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        this.listLoading = false
      })
    }
  }
}
</script>
<style scoped>
.filter-container{
  padding:20px 20px 10px;
  display: flex;
}

.upload-demo{
  margin: 20px 0 10px;
}
</style>
